#!/bin/sh
#
###################################################################################################
#
# lddcheck.sh A bash script using ldd to check certain locations for binaries missing libs
#
# Copyright  2006 nordle (nordle  ++A T++  nordle++dottt++org)
#
# This program is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
###################################################################################################



# Set the locations of the files to contain the search results
# LDD1 is a staging area eg /tmp/LDD1.txt
# LDD2 is the presented results eg /tmp/LDD2.txt
# EDIT THIS
LDD1="/tmp/lddcheck1.txt"
LDD2="/tmp/lddcheck2.txt"

#############################################################################
# Should not need to edit the below for it to work.
# Although definitely change the searched direcotries to match your system.

# No debugging
set +x

# Script version number
VERSION=0.10.0

# Remove old files if they exist
if [ -e $LDD1 ]
  then
    rm $LDD1
fi

if [ -e $LDD2 ]
  then
    rm $LDD2
fi

# Loop through system directories, test files to see if they are executable and readable
# If they are then store the filename and the result of ldd test
echo
echo "This may take several minutes..."
echo
for DIR in /usr/bin /usr/lib /usr/libexec; do
  if [ -d $DIR ]; then
    find "$DIR" -type f -perm -540 |
    while read FILE; do
      echo $FILE >> $LDD1; ldd $FILE | grep "not found" >> $LDD1;
    done
  fi
done

# Export the result to screen along with suggestions
cat $LDD1 | grep -B1 "not found" >> $LDD2
cat $LDD2

echo
echo "Version: $VERSION"
echo
echo "Completed test, results stored in $LDD2 and displayed above"
echo
exit 0
